//
//  EncryptAction.m
//  FinancialStore
//
//  Created by zhenqiu on 16/7/19.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "EncryptAction.h"
#import "NSString+MD5.h"

@interface EncryptAction(){
     NSString * auth_key;
}


@end


@implementation EncryptAction


-(instancetype)init{
    
    if (self = [super init]) {
        // do do
    }
    return self;
}

+(id)getAuthKey{
    
    id key = [[NSUserDefaults standardUserDefaults] valueForKey:AUTH_CACHE_AUTH_KEY];
    return key? key:AUTH_KEY;
    
}


+(void)cacheAuthKey:(id)authKey{
    
    [[NSUserDefaults standardUserDefaults] setValue:authKey forKey:AUTH_CACHE_AUTH_KEY];
    
}

+(void)removeAuthKey{
  [[NSUserDefaults standardUserDefaults] setValue:nil forKey:AUTH_CACHE_AUTH_KEY];
  [[NSUserDefaults standardUserDefaults] setValue:nil forKey:AUTH_CACHE_DID_KEY];
}

+(id)getAuthDid{
    
    id did = [[NSUserDefaults standardUserDefaults]  valueForKey:AUTH_CACHE_DID_KEY];
    return did ? did : AUTH_DID;
}


+(void)cacheAuthDid:(id)authDid{
    
    [[NSUserDefaults standardUserDefaults] setValue:authDid forKey:AUTH_CACHE_DID_KEY];
    
}


+(id)genAuthDsig:(NSString*)timeStr{
    
    NSString * auth_dsig = nil;
    NSMutableString * souce = [NSMutableString stringWithCapacity:20];
    [souce appendString:timeStr];
    [souce appendString:[self getAuthKey]];
    
    auth_dsig = [souce MD5Digest];
    auth_dsig = [auth_dsig substringWithRange:NSMakeRange(8, 16)];
    auth_dsig = [auth_dsig lowercaseString];
    return auth_dsig;
}


@end
